package com.cloud.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.business.dto.SaveDTO;
import com.cloud.business.dto.SelectDTO;
import com.cloud.business.entity.DemoServerSeataTable;
import com.cloud.business.feign.DemoClientFeign;
import com.cloud.business.mapper.DemoMapper;
import com.cloud.business.mapper.DemoSeataMapper;
import com.cloud.business.service.DemoService;
import com.cloud.business.entity.DemoServerTable;
import com.cloud.business.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Slf4j
@Service
public class DemoServiceImpl extends ServiceImpl<DemoMapper, DemoServerTable> implements DemoService {

    @Autowired
    private DemoSeataMapper seataMapper;

    @Autowired
    private DemoClientFeign demoClientFeign;

    @Override
    public Page<DemoServerTable> selectList(SelectDTO dto) {
        LambdaQueryWrapper<DemoServerTable> queryWrapper = Wrappers.lambdaQuery(DemoServerTable.class);
        queryWrapper.eq(StrUtil.isNotEmpty(dto.getName()), DemoServerTable::getName, dto.getName());
        return this.page(dto.getPage(), queryWrapper);
    }

    @Transactional
    @Override
    public boolean save(SaveDTO saveDTO) {
        DemoServerTable demo = BeanUtil.toBean(saveDTO, DemoServerTable.class);
        DemoServerSeataTable demoSeata = BeanUtil.toBean(saveDTO, DemoServerSeataTable.class);
        seataMapper.insert(demoSeata);
        boolean r = this.save(demo);
        //int i = 1/0;
        return r;
    }

    @Override
    public boolean saveSeata(SaveDTO saveDTO) {
        DemoServerTable demo = BeanUtil.toBean(saveDTO, DemoServerTable.class);
        boolean r = this.save(demo);
        ResultVO clienR = demoClientFeign.save(saveDTO);
        log.info("远程调用结果={}",clienR);
        //远程调用
        return r;
    }
}
